package org.mtdownload.listview;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumnModel;

import org.mtdownload.download.DownloadManager;
import org.mtdownload.download.DownloadTask;
import org.mtdownload.utils.PreferencesManager;

public class MTDownloadListView extends JPanel {

	private static final long serialVersionUID = 3257567308670382905L;
	
	private JScrollPane scroll=new JScrollPane();
	private JTable table=new JTable();
	private DownloadsListTableModel model=new DownloadsListTableModel();
	
	private List<DownloadsListSelectionListener> listSelectionListeners=new ArrayList<DownloadsListSelectionListener>(); 
	
	public MTDownloadListView() {
		super();
		init();
	}
	
	private void init(){
		setLayout(new BorderLayout());
		table.setModel(model);
		scroll.setViewportView(table);
		add(scroll,BorderLayout.CENTER);
		for(int i=0;i<DownloadManager.getDownloadTasksListSize();i++){
			model.addDownloadTask(DownloadManager.getDownloadTaskAt(i));
		}
		initColumnWidths();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setShowVerticalLines(false);
		table.setColumnSelectionAllowed(false);
		String databaseFile=PreferencesManager.getDownloadListDatabase();
		DownloadManager.loadDownloadList(databaseFile);
		for (int i=0;i<DownloadManager.getDownloadTasksListSize();i++){
			model.addDownloadTask(DownloadManager.getDownloadTaskAt(i));
		}
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				int[] rows=table.getSelectedRows();
				notifyDownloadListSelectionListeners(rows);
			}
		});
	}
	
	public void removeDownloadTask(DownloadTask task){
		model.removeDownloadTask(task);
	}
	
	public void addDownloadListSelectionListener(DownloadsListSelectionListener l){
		listSelectionListeners.add(l);
	}
	public void removeDownloadListSelectionListener(DownloadsListSelectionListener l){
		listSelectionListeners.remove(l);
	}
	private void notifyDownloadListSelectionListeners(int[] rows){
		for(DownloadsListSelectionListener l:listSelectionListeners){
			l.downloadsSelected(rows);
		}
	}
	
	private void initColumnWidths() {
		TableColumnModel colModel=table.getColumnModel();
		//{"!","File","Status","Percent","Speed","Completed/Total","ETA","Elapsed","Threads","URL"};
		//  0 , 1    , 2      , 3       , 4     , 5               , 6   , 7       , 8       , 9
		colModel.getColumn(0).setPreferredWidth(20);
		//colModel.getColumn(0).setMaxWidth(25);
		//colModel.getColumn(0).setMinWidth(10);
		colModel.getColumn(1).setPreferredWidth(200);
		colModel.getColumn(2).setPreferredWidth(100);
		colModel.getColumn(3).setPreferredWidth(70);
		colModel.getColumn(4).setPreferredWidth(80);
		colModel.getColumn(5).setPreferredWidth(80);
		colModel.getColumn(6).setPreferredWidth(80);
		colModel.getColumn(7).setPreferredWidth(80);
		colModel.getColumn(8).setPreferredWidth(60);
		colModel.getColumn(9).setPreferredWidth(200);
		
	}

}
